<?php
// +------------------------------------------------------------------------+
// | class.upload.php                                                       |
// +------------------------------------------------------------------------+
class Vfriend_Upload 
{	
	public function process($oFile, $strUploadDir = '', $intMaxSize = IMAGE_SIZE, $arrType = null, $strName = '') 
	{		
		if (!isset( $oFile ) || is_null ( $oFile['tmp_name'] ) || $oFile['name'] == '')
			return array ('error' => 8);
		if (is_null($arrType)) {
			$arrType = array('image/pjpeg', 'image/jpg', 'image/jpeg', 'image/gif', 'image/png');
		}
		
		switch ($oFile ["error"]) {
			case 0 :
				break;
			case 1 :
				return array ('error' => 1);
				break;
			case 2 :
				return array ('error' => 2);
				break;
			case 3 :
				return array ('error' => 3);
				break;
			case 4 :
				return array ('error' => 4);
				break;
			case 5 :
				return array ('error' => 5);
				break;
			case 6 :
				return array ('error' => 6);
				break;
			default :
				return array ('error' => 7);
				break;
		}		
		if (!in_array($oFile['type'], $arrType)) {		
			return array ('error' => 9);
		}
		if ($oFile['size'] <= 0 || $oFile['size'] > $intMaxSize) {
			return array ('error' => 10);
		}
		if( substr($strUploadDir, -1, 1) != '/' ) $strUploadDir .= '/';
		Vfriend_Function::mkDirectory($strUploadDir);
		if ($strName != '')
			$strFileName = $strName . strtolower(strrchr($oFile['name'], '.'));
		else
			$strFileName = rand(100, 999) . time() . strtolower(strrchr($oFile['name'], '.'));		
		$strFullPath = $strUploadDir . $strFileName;
		move_uploaded_file($oFile['tmp_name'], $strFullPath);
		$arrSize 	= @getimagesize($strFullPath);
		$intWidth 	= 0;
		$intHeight 	= 0;
		if(is_array($arrSize) && count($arrSize)) {
			$intWidth 	=  $arrSize[0];
			$intHeight	=  $arrSize[1];
		}
		return array (
			'error' 	=> 0,
			'name' 		=> $strFileName,
			'fullpath' 	=> $strFullPath,
			'width'  	=> $intWidth,
			'height' 	=> $intHeight
		);		
	}
	public function uploadCover($file, &$strPath='') {
		if ($file['name'] != '') {
			$strFolder			= date('Y/m/d/');
			$strFullFolder 		= DIR_COVER_FULL . $strFolder;
			$strThumbFolder 	= DIR_COVER_THUMB . $strFolder;
			$strSmallFolder 	= DIR_COVER_SMALL . $strFolder;
			$arrUploadResult 	= Vfriend_Upload::process($file, $strFullFolder, IMAGE_SIZE*1024*1024);			
			if ($arrUploadResult['error'] == 0) {
				$strPath		= $strFolder . $arrUploadResult['name'];
				$intHeight		= $arrUploadResult['height'];
				$strFullPath 	= $arrUploadResult['fullpath'];								
				$strFileName	= $arrUploadResult['name'];
				$strThumbPath	= $strThumbFolder . $strFileName;
				$strSmallPath	= $strSmallFolder . $strFileName;				
				$objThumb 		= new Vfriend_Thumbnail($strFullPath);
				$objThumb->quality 		  	= 100;
				$objThumb->output_format 	= 'JPG,JPEG,jpg,jpeg,GIF,gif';
				$objThumb->jpeg_progressive = 1;
				$objThumb->bicubic_resample = false;
				$objThumb->allow_enlarge	= false;
				$objThumb->size_width(COVER_IMAGE_FULL_WIDTH);
				$objThumb->process();
				$objThumb->save($strFullPath);	
				
				Vfriend_Function::mkDirectory($strThumbFolder);		
				Vfriend_Function::mkDirectory($strSmallFolder);	
				Vfriend_Crop::thumbSquare($strFullPath, $strThumbPath, COVER_IMAGE_FULL_WIDTH, COVER_IMAGE_FULL_HEIGHT);				
				Vfriend_Crop::thumbSquare($strFullPath, $strSmallPath, COVER_IMAGE_SMALL_WIDTH, COVER_IMAGE_SMALL_HEIGHT);				
			}
			return $arrUploadResult;
		}
		return null;
	}	
	public function uploadCoverThumb($strPath, $strPathThumb, $intXpos, $intYpos, $intThumbWidth=791, $intThumbHeight=132) {
		$strFullPath 	= DIR_COVER_FULL . $strPath;
		$strThumbPath 	= DIR_COVER_THUMB . $strPathThumb;			
		Vfriend_Crop::cropImage($strFullPath, $strThumbPath, $intXpos, $intYpos, $intThumbWidth, $intThumbHeight);
	}
	public function uploadPlaceImg($file, &$strPath='') {
		if ($file['name'] != '') {
			$strFolder			= date('Y/m/d/');			
			$strFullFolder 		= DIR_PLACE_FULL . $strFolder;
			$strThumbFolder 	= DIR_PLACE_THUMB . $strFolder;	
			$strSmallFolder 	= DIR_PLACE_SMALL . $strFolder;				
			$arrUploadResult 	= Vfriend_Upload::process($file, $strFullFolder, IMAGE_SIZE*1024*1024);			
			if ($arrUploadResult['error'] == 0) {
				$strPath		= $strFolder . $arrUploadResult['name'];
				$intHeight		= $arrUploadResult['height'];
				$strFullPath 	= $arrUploadResult['fullpath'];								
				$strFileName	= $arrUploadResult['name'];
				$strThumbPath	= $strThumbFolder . $strFileName;							
				$strSmallPath	= $strSmallFolder . $strFileName;							
				if($arrUploadResult['width'] > PLACE_IMAGE_FULL_WIDTH) {
					$objThumb 					= new Vfriend_Thumbnail($strFullPath);					
					$objThumb->quality 		  	= 100;
					$objThumb->output_format 	= 'JPG,JPEG,jpg,jpeg,GIF,gif';
					$objThumb->jpeg_progressive = 1;
					$objThumb->bicubic_resample = false;
					$objThumb->allow_enlarge	= false;
					$objThumb->size_width(PLACE_IMAGE_FULL_WIDTH);
					$objThumb->process();
					$objThumb->save($strFullPath);	
				}
				
				Vfriend_Function::mkDirectory($strThumbFolder);					
				Vfriend_Function::mkDirectory($strSmallFolder);					
				Vfriend_Crop::thumbSquare($strFullPath, $strThumbPath, PLACE_IMAGE_THUMB_WIDTH, PLACE_IMAGE_THUMB_HEIGHT);							
				Vfriend_Crop::thumbSquare($strFullPath, $strSmallPath, PLACE_IMAGE_SMALL_WIDTH, PLACE_IMAGE_SMALL_HEIGHT);							
			}
			return $arrUploadResult;
		}
		return null;
	}
		
}
?>
